package cn.tedu.ivos.user.controller;

import cn.tedu.ivos.base.response.JsonResult;

import cn.tedu.ivos.user.pojo.dto.UserLoginParam;
import cn.tedu.ivos.user.pojo.dto.UserQuery;
import cn.tedu.ivos.user.pojo.dto.UserSaveParam;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v1/user")
public class UserController {
    @Autowired
    UserService userService;
    @PostMapping("login")
    public JsonResult login(UserLoginParam userLoginParam){
        log.debug("用户登录:userLoginParam={}",userLoginParam);
        //Alt+Enter 提示生成方法
        UserVO userVO = userService.login(userLoginParam);
        return JsonResult.ok(userVO);
    }

    @PostMapping("save")
    //前端传过来的参数已经由qs(Querystring)转为了查询字符串格式,所以不需要@RequestBody注解进行映射了
    //public JsonResult save(@RequestBody UserSaveParam userSaveParam){
    public JsonResult save(UserSaveParam userSaveParam){
        log.debug("保存用户:userSaveParam={}",userSaveParam);
        userService.saveUser(userSaveParam);
        return JsonResult.ok();
    }

    @GetMapping("select")
    public JsonResult selectUser(UserQuery userQuery){
        log.debug("查询用户:userQuery={}",userQuery);
        List<UserVO> list = userService.selectUser(userQuery);
        return JsonResult.ok(list);
    }
    @PostMapping("update/password/{id}")
    public JsonResult resetPassword(@PathVariable Long id){
        log.debug("重置密码:id={}",id);
        userService.resetPassword(id);
        return JsonResult.ok();
    }

    @PostMapping("update/status/{id}/{status}")
    public JsonResult updateStatus(@PathVariable Long id,@PathVariable String status){
        log.debug("更新用户状态:id={},status={}",id,status);
        userService.updateStatus(id,status);
        return JsonResult.ok();
    }
    @PostMapping("delete/{id}")
    public JsonResult deleteUser(@PathVariable Long id){
        log.debug("删除用户:id={}",id);
        userService.deleteUser(id);
        return JsonResult.ok();
    }

    @GetMapping("select/audit/{parentId}")
    public JsonResult selectAuditUser(@PathVariable Long parentId){
        log.debug("查询审核人:parentId={}",parentId);
        List<UserVO> list = userService.selectAuditUser(parentId);
        return JsonResult.ok(list);
    }

}
